﻿<!DOCTYPE html>
<html>
<head>
	<script src="http://code.jquery.com/jquery-1.11.2.js" type="text/javascript"></script>
	<script src="../../jsrender.js" type="text/javascript"></script>
	<link href="../resources/demos.css" rel="stylesheet" type="text/css" />

	<link href="../resources/movielist.css" rel="stylesheet" type="text/css" />
	<style>
	pre { font-size:10pt; font-weight:bold; }
	</style>
</head>
<body>
<a href="../demos.html">JsRender Demos</a><br />

<h3>Example Scenario: providing default values for data.</h3>

<!---------------------- First Example ---------------------->

<div class="subhead">The simplest (and best) way: Javascript expression '||':</div>

<pre>
{{>languages||'Languages unavailable'}}
</pre>

<table>
	<thead><tr><th>Title</th><th>{{>path}}</th></tr></thead>
	<tbody id="movieList1"></tbody>
</table>

<!---------------------- Second Example ---------------------->

<div class="subhead">Creating a special custom tag:</div>

<pre>
{{get languages defaultValue="No languages!"/}}

$.views.tags({
    get: function( value ) {
        return value || this.ctx.props.defaultValue;
    }
});
</pre>

<table>
	<thead><tr><th>Title</th><th>{{get path default="..."}}</th></tr></thead>
	<tbody id="movieList2"></tbody>
</table>

<!---------------------- Third Example ---------------------->

<div class="subhead">Creating a multi-purpose utility tag:</div>

<pre>
{{yesNo languages yes="Alternate languages available:" no="No alternate languages"/}}

$.views.tags({
    yesNo: function( value ) {
        return value ? this.tagCtx.props.yes : this.tagCtx.props.no;
    }
});
</pre>

<table>
	<thead><tr><th>Title</th><th>{{yesNo path yes="..." no="..."}}</th></tr></thead>
	<tbody id="movieList3"></tbody>
</table>

<!--=================== Demo ===================-->

<!------------------ Templates ------------------>

<script id="movieTemplate1" type="text/x-jsrender">
	<tr>
		<td>{{>title}}</td>
		<td>
			{{>languages||'Languages unavailable'}}
		</td>
	</tr>
</script>

<script id="movieTemplate2" type="text/x-jsrender">
	<tr>
		<td>{{>title}}</td>
		<td>
			{{get languages defaultValue="No languages!"/}}
		</td>
	</tr>
</script>

<script id="movieTemplate3" type="text/x-jsrender">
	<tr>
		<td>{{>title}}</td>
		<td>
			{{yesNo languages yes="Alternate languages available:" no="No alternate languages"/}}
			<em>{{>languages}}</em>
		</td>
	</tr>
</script>

<!------------------ Script ------------------>

<script type="text/javascript">

	$.views.tags({
		get: function( value ) {
			return value || this.tagCtx.props.defaultValue;
		},

		yesNo: function( value ) {
			return value ? this.tagCtx.props.yes : this.tagCtx.props.no;
		}
	});

	var movies = [
		{
			title: "Meet Joe Black",
			languages: "English and French"
		},
		{
			title: "Eyes Wide Shut"
		},
		{
			title: "The Mighty"
		},
		{
			title: "City Hunter",
			languages: "Mandarin and Chinese"
		}
	];

	$( "#movieList1" ).html(
		$( "#movieTemplate1" ).render( movies )
	);

	$( "#movieList2" ).html(
		$( "#movieTemplate2" ).render( movies )
	);

	$( "#movieList3" ).html(
		$( "#movieTemplate3" ).render( movies )
	);
</script>

</body>
</html>